/*
 * Copyright (C) 2012  E-dance Studio Harbin Institute of Technology 
 * Project:campusmobile
 * Author: 初楷博
 * Create Date: 2012-5-25下午08:54:30
 */
package hit.edance.campusmobile.chat.activity;

import hit.edance.campusmobile.chat.R;
import hit.edance.campusmobile.chat.adapter.FriendsExpandableListAdapter;
import hit.edance.campusmobile.chat.listener.MyRosterListener;
import hit.edance.campusmobile.chat.model.ActivityKiller;
import hit.edance.campusmobile.chat.model.FriendsManager;
import hit.edance.campusmobile.chat.model.OfflineManager;
import hit.edance.campusmobile.chat.model.User;
import hit.edance.campusmobile.chat.service.CampusMobileService;

import org.jivesoftware.smack.Roster;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import android.widget.ExpandableListView.OnChildClickListener;

/**
 * 好友列表界面
 * 
 * @Project campusmobile
 * @Package hit.edance.campusmobile.chat.activity
 * @Class ActivityFriends
 * @Date 2012-5-25 下午08:54:30
 * @author 初楷博
 * @version 1.0
 * @since 1.0
 */
public class ActivityFriends extends Activity {

	/**
	 * 好友功能对话框ID
	 */
	private final int DIALOG_FOR_CHILD = 0;
	/**
	 * 分组功能对话框ID
	 */
	private final int DIALOG_FOR_GROUP = 1;

	private Handler myhandler;
	private ExpandableListView list;
	private Roster roster;
	private FriendsExpandableListAdapter friendsAdapter;
	private MyRosterListener rosterListener;

	boolean click_tag = false;
	int groupPos;
	int childPos;

	/*
	 * @see android.app.Activity#onCreate(android.os.Bundle)onCreate
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		ActivityKiller.addActivity(hashCode(), this);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chat_friends);
		myhandler = new Handler();
		initActivity();
	}

	/**
	 * 界面初始化
	 */
	private void initActivity() {
		list = (ExpandableListView) findViewById(R.id.friendlist);

		roster = CampusMobileService.connection.getRoster();
		friendsAdapter = new FriendsExpandableListAdapter(this, roster);
		list.setAdapter(friendsAdapter);
		rosterListener = new MyRosterListener(this, friendsAdapter,myhandler);
		roster.addRosterListener(rosterListener);
		registerForContextMenu(list);
		list.setOnChildClickListener(new OnChildClickListener() {

			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// 进入聊天界面！！！
				if (click_tag == true)
					return false;
				else {
					// 获取用户，跳到聊天室
					User user = FriendsManager
							.getInstance()
							.getGroupChildMap()
							.get(FriendsManager.getInstance().getGroups()
									.get(groupPosition).getName())
							.get(childPosition);
					Intent intent = new Intent(ActivityFriends.this,
							ActivityChat.class);
					intent.putExtra("nickname", user.getNickname());
					intent.putExtra("jabberId", user.getJabberId());
					startActivity(intent);
					return true;
				}
			}
		});
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {

		click_tag = true;

		try {
			ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuInfo;
			int type = ExpandableListView
					.getPackedPositionType(info.packedPosition);
			// 点击的是子列表
			if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
				groupPos = ExpandableListView
						.getPackedPositionGroup(info.packedPosition);
				childPos = ExpandableListView
						.getPackedPositionChild(info.packedPosition);
				showDialog(DIALOG_FOR_CHILD);

			}
			// 点击的是组列表
			else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
				groupPos = ExpandableListView
						.getPackedPositionGroup(info.packedPosition);
				showDialog(DIALOG_FOR_GROUP);
			}
		} finally {
			click_tag = false;
		}
	}

	/*
	 * @see android.app.Activity#onPrepareDialog(int, android.app.Dialog)
	 * onPrepareDialog
	 */
	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		super.onPrepareDialog(id, dialog);
		switch (id) {
		case DIALOG_FOR_CHILD:
			dialog.setTitle(FriendsManager
					.getInstance()
					.getGroupChildMap()
					.get(FriendsManager.getInstance().getGroups().get(groupPos)
							.getName()).get(childPos).getNickname());
			break;
		case DIALOG_FOR_GROUP:
			dialog.setTitle(FriendsManager.getInstance().getGroups()
					.get(groupPos).getName());
			break;
		default:
			break;
		}
	}

	/*
	 * @see android.app.Activity#onCreateDialog(int)onCreateDialog
	 */
	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		Builder builder = new AlertDialog.Builder(this);
		switch (id) {
		case DIALOG_FOR_CHILD:
			final String[] childitems = new String[] {
					getResources().getString(R.string.chat_watchinfo),
					getResources().getString(R.string.chat_movefriend),
					getResources().getString(R.string.chat_chathistory),
					getResources().getString(R.string.chat_delfriend) };
			dialog = builder
					.setTitle(
							FriendsManager
									.getInstance()
									.getGroupChildMap()
									.get(FriendsManager.getInstance()
											.getGroups().get(groupPos)
											.getName()).get(childPos)
									.getNickname())
					.setItems(childitems, new OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							switch (which) {
							case 0: // TODO
								Toast.makeText(ActivityFriends.this, "详细信息待定！", Toast.LENGTH_SHORT).show();
								break;
							case 1:// TODO
							case 2:
								watchChatHistory(FriendsManager
										.getInstance()
										.getGroupChildMap()
										.get(FriendsManager.getInstance()
												.getGroups().get(groupPos)
												.getName()).get(childPos)
										.getJabberId());
								break;
							case 3:// TODO
							default:
								break;
							}
						}

					})
					.setNegativeButton(
							getResources().getString(R.string.chat_cancel),
							null).create();
			break;
		case DIALOG_FOR_GROUP:
			final String[] groupitems = new String[] {
					getResources().getString(R.string.chat_renamegroup),
					getResources().getString(R.string.chat_delgroup) };
			dialog = builder
					.setTitle(
							FriendsManager.getInstance().getGroups()
									.get(groupPos).getName())
					.setItems(groupitems, new OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							switch (which) {
							case 0: // TODO
							case 1:// TODO
							default:
								break;
							}

						}

					})
					.setNegativeButton(
							getResources().getString(R.string.chat_cancel),
							null).create();
			break;
		default:
			break;
		}
		return dialog;
	}

	/**
	 * 查看聊天记录
	 * 
	 * @param jabberId
	 */
	private void watchChatHistory(String jabberId) {
		Intent chatHistory = new Intent(ActivityFriends.this,
				ActivityChatHistory.class);
		chatHistory.putExtra("jid", jabberId);
		chatHistory.putExtra(
				"nickname",
				FriendsManager
						.getInstance()
						.getGroupChildMap()
						.get(FriendsManager.getInstance().getGroups()
								.get(groupPos).getName()).get(childPos)
						.getNickname());
		startActivity(chatHistory);
	}

	private Runnable flushList = new Runnable() {

		public void run() {
			friendsAdapter.notifyDataSetChanged();
		}

	};
	/* @see android.app.Activity#onDestroy()
	 *onDestroy
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		roster.removeRosterListener(rosterListener);
		ActivityKiller.killActivityByHashCode(hashCode());
		super.onDestroy();
	}
}
